Component({
  properties: {
    percentage: {
      type: Number,
      value: 0,
    },
    time: {
      type: Number,
      value: 0
    }
  },

  data: {
    // 组件内部数据
    closeAlertFnType: false,
    dateVisible: false,
    alertVisible: false,
    alertJgVisible: false,
    currentTime: '请选择',
    currentalertJgValue: '一小时',
    currentAlertValue: '20%',
    startData: [{
        label: '08',
        value: '08'
      },
      {
        label: '09',
        value: '09'
      },
      {
        label: '10',
        value: '10'
      },
      {
        label: '11',
        value: '11'
      },
      {
        label: '12',
        value: '12'
      },
      {
        label: '13',
        value: '13'
      },
      {
        label: '14',
        value: '14'
      },
      {
        label: '15',
        value: '15'
      },
      {
        label: '16',
        value: '16'
      },
      {
        label: '17',
        value: '17'
      },
      {
        label: '18',
        value: '18'
      },
      {
        label: '19',
        value: '19'
      },
      {
        label: '20',
        value: '20'
      },
      {
        label: '21',
        value: '21'
      },
    ],
    endData: [{
        label: '08',
        value: '08'
      },
      {
        label: '09',
        value: '09'
      },
      {
        label: '10',
        value: '10'
      },
      {
        label: '11',
        value: '11'
      },
      {
        label: '12',
        value: '12'
      },
      {
        label: '13',
        value: '13'
      },
      {
        label: '14',
        value: '14'
      },
      {
        label: '15',
        value: '15'
      },
      {
        label: '16',
        value: '16'
      },
      {
        label: '17',
        value: '17'
      },
      {
        label: '18',
        value: '18'
      },
      {
        label: '19',
        value: '19'
      },
      {
        label: '20',
        value: '20'
      },
      {
        label: '21',
        value: '21'
      },
    ],
    alertList: [{
        label: '10%',
        value: '10'
      },
      {
        label: '20%',
        value: '20'
      },
      {
        label: '30%',
        value: '30'
      },
    ],
    alertJgList: [{
        label: '1小时',
        value: '01'
      },
      {
        label: '2小时',
        value: '02'
      },
      {
        label: '4小时',
        value: '04'
      },
    ],
    openAlert: true,
    currentalertJgValueRef: '01',
    currentAlertValueRef: '20',
  },

  lifetimes: {
    attached: function () {
      let _this = this;
      // 在组件实例进入页面节点树时执行
      wx.getStorage({
        key: 'alertList',
        success(res) {
          let {
            openAlert,
            currentAlertValueRef,
            currentTimeValue,
            currentalertJgValueRef
          } = res.data;
          _this.setData({
            openAlert,
            currentAlertValueRef,
            currentAlertValue: currentAlertValueRef + '%',
            currentTimeValue,
            // 若 currentTimeValue 是四位字符串，在中间拼接一个‘-’
            currentTime: currentTimeValue.slice(0, 2) + '-' + currentTimeValue.slice(2),
            currentalertJgValueRef,
            currentalertJgValue: currentalertJgValueRef == '01' ? '一小时' : currentalertJgValueRef == '02' ? '两小时' : currentalertJgValueRef == '04' ? '四小时' : '一小时',
          })
        }
      })
    },
    ready: function () {
      // 在组件在视图层布局完成后执行
    }
  },

  methods: {
    openAlertFn(e) {
      this.setData({
        openAlert: e.detail.value,
      })
    },
    alertSetFn() {
      if (!this.data.currentTimeValue) {
        wx.showToast({
          title: '请选择报警时间范围',
          icon: 'none'
        })
        return;
      }
      let {
        openAlert,
        currentAlertValueRef,
        currentTimeValue,
        currentalertJgValueRef
      } = this.data;
      let setCon = `SET:13;${openAlert?'01':'00'};${currentAlertValueRef};${currentTimeValue};${currentalertJgValueRef};000;@`;
      let data = {
        openAlert,
        currentAlertValueRef,
        currentTimeValue,
        currentalertJgValueRef
      }
      wx.setStorage({
        key: 'alertList',
        data
      });
      this.triggerEvent('alertSetFn', setCon);
    },
    closeFn() {
      this.setData({
        closeAlertFnType: true,
      })
      this.triggerEvent('closeAlertFn')
    },
    alertFn() {
      this.setData({
        alertVisible: true,
      })
    },
    alertJgFn() {
      this.setData({
        alertJgVisible: true,
      })
    },
    alertDataFn() {
      this.setData({
        dateVisible: true,
      })
    },
    onAlertChange(e) {
      const {
        label,
        value
      } = e.detail;
      this.setData({
        currentAlertValue: label[0],
        currentAlertValueRef: value[0],
      })
    },
    onAlertJgChange(e) {
      const {
        label,
        value
      } = e.detail;
      this.setData({
        currentalertJgValue: label[0],
        currentalertJgValueRef: value[0]
      })
    },
    onColumnChange(e) {
      const {
        label
      } = e.detail;
      //如果label[03,11] 里面开始时间大于结束时间，就提示开始时间不能大于结束时间
      if (Number(label[0]) >= Number(label[1])) {
        wx.showToast({
          title: '开始时间不能大于结束时间',
          icon: 'none',
          duration: 2000
        })
        return
      }
      //如果不是，就把 label 值转换成 03-11 的格式
      const currentTime = `${label[0]}-${label[1]}`;
      this.setData({
        currentTime,
        currentTimeValue: `${label[0]}${label[1]}`
      })
    }

  }
})